<?php

namespace App\Http\Controllers\Api;

use App\Model\SuggestRoomList;
use Illuminate\Http\Request;

class SuggestRoomController extends BaseController
{
    protected $suggestRoomListModel;//推荐房间列表

    public function __construct()
    {
        $this->suggestRoomListModel = new SuggestRoomList();
    }

    //获取首页推荐房间列表
    public function suggestList(Request $request){
        $page = $request->get('page',1);//页数
        $limit = $request->get('limit',10);//条数
        $data = $this->suggestRoomListModel->getSuggestList($page,$limit);
        return $this->toJson(self::SUCCESS, $data);
    }

    //搜索结果
    public function searchRoom(Request $request){
        $kwd = $request->get('kwd',0);
        $page = $request->get('page',1);//页数
        $limit = $request->get('limit',10);//条数
        if(empty($kwd)){
            return $this->toJson(self::PARAM_ERROR);
        }
        $data = $this->suggestRoomListModel->searchRoom($kwd, $page, $limit);
        return $this->toJson(self::SUCCESS, $data);
    }
    //更改显示状态或者删除
    public function changeStatus(Request $request){
        $chageId = $request->post('id',0);
        $showStatus = $request->post('show_status',null);//1=显示 0=不显示
        $isDelete = $request->post('is_delete',null);//1=已删除 0=未删除
        if(empty($chageId)){
            return $this->toJson(self::PARAM_ERROR);
        }
        $res = $this->suggestRoomListModel->changeStatus($chageId, $showStatus, $isDelete);
        if($res['code'] != 0){
            return $this->toJson(self::ERROR, $res['msg']);
        }
        return $this->toJson(self::SUCCESS);
    }

    //添加数据/更改
    public function addOrUpdate(Request $request){
        $id = $request->post('id',0);//编辑id
        $room_id = $request->post('room_id',0);//房间id
        $start_time = $request->post('start_time','');//开始时间
        $end_time = $request->post('end_time','');//结束时间
        $sort = $request->post('sort',0);//权重
        $is_show = $request->post('is_show',1);//1=显示 0=不显示

        if(empty($start_time) || empty($end_time) || empty($sort) || empty($room_id) ){
            return $this->toJson(self::PARAM_ERROR);
        }
        $res = $this->suggestRoomListModel->addOrUpdate($id, $room_id, $start_time, $end_time, $sort,$is_show);
        if($res['code'] != 0){
            return $this->toJson(self::ERROR, $res['msg']);
        }
        return $this->toJson(self::SUCCESS);
    }
}